package com.ls.linked_list;

public class DoubleLinkedList {

    public static void main(String[] args) {
        HeroNode2 h1 = new HeroNode2(1, "宋江", "");
        HeroNode2 h2 = new HeroNode2(2, "卢俊义", "");
        HeroNode2 h3 = new HeroNode2(3, "吴用", "");
        HeroNode2 h4 = new HeroNode2(4, "林冲", "");
        HeroNode2 u0 = new HeroNode2(2, "西瓜", "");
        HeroNode2 u1 = new HeroNode2(10, "豆豆", "");
        DoubleLinkedList linkedList = new DoubleLinkedList();
        linkedList.add(h1);
        linkedList.add(h2);
        linkedList.add(h3);
        linkedList.add(h4);
        linkedList.show();
        System.out.println("------");

        linkedList.update(u0);
        linkedList.update(u1);
        linkedList.delete(4);
        linkedList.delete(9);
        linkedList.show();


        int length = linkedList.getLength();
        System.out.println(length);




    }

    public int getLength() {

        if (head.next == null) {
            return 0;
        }
        int length = 0;
        HeroNode2 cur = head.next;
        while (cur != null) {
            length++;
            cur = cur.next;
        }
        return length;


    }
    
    //双向链表,我们可以直接找到要删除的节点,找到后自我删除即可
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("empty");
            return;
        }
        HeroNode2 cur = head.next;
        boolean flag = false;//是否找到删除节点的前一个
        while (true) {
            if (cur == null) {
                break;
            }
            if (cur.no == no) {
                flag = true;
                break;
            }
            cur = cur.next;
        }
        if (flag) {
            cur.pre.next = cur.next;
            if (cur.next != null) {
                cur.next.pre = cur.pre;
            }

        } else {
            System.out.println("未找到节点");
        }
    }


    public void update(HeroNode2 newNode) {

        if (head.next == null) {
            System.out.println("empty");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == newNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = newNode.name;
            temp.nickName = newNode.nickName;
        } else {
            System.out.println("未找到节点");
        }
    }


    public void add(HeroNode2 node) {

        HeroNode2 temp = head;
        //遍历链表找到最后
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = node;
        node.pre = temp;
    }


    //显示链表
    public void show() {
        if (head.next == null) {
            System.out.println("empty");
            return;
        }
        HeroNode2 temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }
            System.out.println(temp.toString());
            temp = temp.next;
        }
    }

    //初始化头节点,头节点存放具体数据
    private HeroNode2 head = new HeroNode2(0, "", "");
}


//英雄节点
class HeroNode2 {
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 pre;
    public HeroNode2 next;

    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
